package net.danielpaz.pucc.poo.trabalho.model;

/**
 * Classe para manipular dados de objetos da classe funcionario no banco de dados
 * @author Rodolfo
 * @date 14/06/2014
 *
 */

import java.sql.*;

public class FuncionarioDAO {

	public Funcionario func;
	public Motorista mot;
    public Conexao bd;
    private PreparedStatement statement;
    private ResultSet resultSet;
    private String men, sql;
    public static final byte INCLUSAO = 1;
    public static final byte ALTERACAO = 2;
    public static final byte EXCLUSAO = 3;

    public FuncionarioDAO() {
        bd   = new Conexao();
        func = new Funcionario();
        bd.getConnection();
    }
    
    public Funcionario localizar(int id) {
    	
    	sql = "SELECT * FROM FUNCIONARIO WHERE ID = ?";
            
        try {                
            	statement = bd.connection.prepareStatement(sql);
            	statement.setInt(1, id);
                resultSet = statement.executeQuery();
                
                while (resultSet.next()) {
                	 func.setId(Integer.parseInt(resultSet.getString(1)));
                	 func.setNome(resultSet.getString(2));
                	 func.setCpf(resultSet.getString(3));
                	 func.setTelefone(resultSet.getString(4));
                }
                return func;
            }
            catch (Exception erro) {
            	erro.printStackTrace();
            	System.out.println(erro.getMessage());
                return null;
            }	
    }    
    
    
    public String atualizar(int operacao) {
        men = "Operacao realizada com sucesso!";
        try {
            if (operacao==INCLUSAO) {
        		sql = "insert into FUNCIONARIO values (?,?,?,?)";
        		statement = bd.connection.prepareStatement(sql);
        		statement.setString(1, "" + func.getId());
        		statement.setString(2, "" + func.getNome());
        		statement.setString(3, "" + func.getCpf());
        		statement.setString(4, "" + func.getTelefone());
            	
            } else if (operacao==ALTERACAO) {
            	
            	sql = "update funcionario func "
            				+ " set func.nome = ?,"
			  			  	+ " func.cpf = ?, "
			  			  	+ " func.telefone = ?"
			  			  	+ " where func.id = ? ";
            	
            	statement = bd.connection.prepareStatement(sql);
            	statement.setString(1, "" + func.getNome());
            	statement.setString(2, "" + func.getCpf());
            	statement.setString(3, "" + func.getTelefone());
            	statement.setString(4, "" + func.getId());
            	
            } else if (operacao==EXCLUSAO) {
            	sql = "delete from funcionario where id = ?";
            	statement = bd.connection.prepareStatement(sql);
            	statement.setString(1, "" + func.getId());
            }
            
        
            if (statement.executeUpdate() == 0)
                    men = "Falha na operacao!";
            } catch (SQLException erro) {
                men = "Falha na operacao " + erro.toString();
            }
            return men;
            
    }
}
